package com.supreme.threadserver.threadpkg;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.Consumer;

public class SimpleThread implements Runnable, ConsumerHandler<Boolean> {

    private Consumer<Boolean> threadConsumer;

    public SimpleThread() {
    }

    public SimpleThread(Consumer<Boolean> consumer) {
        threadConsumer = consumer;
    }

    @Override
    public void run() {
        Thread thread = Thread.currentThread();
        while (!thread.isInterrupted()) {
            System.out.printf("THREAD = %s DATE = %s RUNNING\n", thread.getName(), LocalDateTime.now());
        }

        System.out.printf("THREAD = %s INTERRUPT = %s EXIT\n", thread.getName(), thread.isInterrupted());
        Optional.ofNullable(this.threadConsumer).ifPresent(consumer -> consumer.accept(true));
    }

    @Override
    public void setConsumer(Consumer<Boolean> consumer) {
        threadConsumer = consumer;
    }
}
